package com.gss.persistance.stock;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.gss.domain.stock.ItemDetailsDTO;
import com.gss.domain.stock.ItemTransactionDTO;


@Repository
public class ItemDAO extends HibernateDaoSupport
{
	Logger logger = Logger.getLogger(ItemDAO.class);
	
	@Autowired
	public ItemDAO( SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
	public boolean saveItem(ItemDetailsDTO detailsDTO)
	{
		boolean isSuccess = false;
		try{
			logger.debug("itemdao"+detailsDTO.getItemStock());
			getHibernateTemplate().save(detailsDTO);
			isSuccess = true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return isSuccess;
	}

	public void loadItemByName(ItemDetailsDTO detailsDTO) {
	
		getHibernateTemplate().load(detailsDTO, detailsDTO.getItemName());
	}

	public boolean updateItemDetails(ItemDetailsDTO itemDetails) {
		boolean isSuccess = false;
		try{
			getHibernateTemplate().update(itemDetails);
			isSuccess = true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return isSuccess;
	}

	public List<ItemDetailsDTO> loadAllItem() {
		
		return getHibernateTemplate().loadAll(ItemDetailsDTO.class);
	}

	public boolean saveTransaction(ItemTransactionDTO itemTransactionDTO) {
		boolean isSuccess = false;
		try{
			getHibernateTemplate().save(itemTransactionDTO);
			isSuccess=true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return isSuccess;
	}

	public void loadItemById(ItemDetailsDTO detailsDTO) {
		getHibernateTemplate().load(detailsDTO, detailsDTO.getItemId());
		
	}

	public List<ItemDetailsDTO> loadItemForLessStock(DetachedCriteria criteria) {
		// TODO Auto-generated method stub
		return getHibernateTemplate().findByCriteria(criteria);
	}

	public List<ItemTransactionDTO> getItemTransaction(DetachedCriteria criteria) {
		return getHibernateTemplate().findByCriteria(criteria);
	}
}
